﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace hrainwebbase.ashx.spfunction
{
    public class simplefunction
    {
        /// <summary>
        /// 从字符串中，非数值码与数值及长度 
        /// 如  A0012F0009 返回 A0012F, 9 ,4(0009)
        /// 如  A0012F0009A  返回 A0012F0009A, 0, 0(空)
        /// 非数值码如果为空，统一返回hcrain
        /// </summary>
        /// <param name="codeinfo"></param>
        /// <returns></returns>
        public static string[] hcrainFunction_getCodeNum(string codeinfo)
        {

            var array = new string[3];
            if (codeinfo == null) return new string[] { "", "0", "0" };
            codeinfo = codeinfo.Trim();
            string baseinfo = codeinfo;
            string numstring = "";
            while (baseinfo.Length > 0)
            {
                string numstringnew = baseinfo.Substring(baseinfo.Length - 1) + numstring;
                if (isNum(numstringnew))
                {
                    numstring = numstringnew;
                    baseinfo = baseinfo.Substring(0, baseinfo.Length - 1);
                }
                else
                {
                    break;
                };
            }
            if (string.IsNullOrEmpty(baseinfo)) baseinfo = "";
            array[0] = baseinfo;
            array[1] = numstring.Length > 0 ? long.Parse(numstring).ToString() : "0";
            array[2] = numstring.Length.ToString();
            return array;
        }
        /// <summary>
        /// 按长度，补0  如4位  传  A  1  返回  A001
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static string getCodeSet0(string start, string end, int length)
        {
            var center = "";

            while (center.Length < (length - start.Length - end.Length))
            {
                center += "0";
            }

            return start + center + end;
        }
        public static bool isNum(string info)
        {
            double num = 0;
            return double.TryParse(info, out num);
        }

    }
}